package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dto.configdata.ContainerDataDTO;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbContainerActiveConfig;
import com.ds.infrastructure.hyperspace.console.entity.table.TbContainerConfig;
import com.ds.infrastructure.hyperspace.console.entity.table.TbMetricsTask;
import com.ds.infrastructure.hyperspace.console.entity.table.TbNodeMetrics;
import com.ds.infrastructure.hyperspace.console.entity.where.TbMetricsTaskCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbNodeMetricsCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.*;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import com.ds.infrastructure.hyperspace.console.utils.MetricsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: yaozhendong
 * @create: 2019-11-04 14:06
 **/

@Service
public class MonitorManageService implements IMonitorManageService {
    @Autowired
    IMetricsTaskService iMetricsTaskService;
    @Autowired
    INodeMetricsService iNodeMetricsService;
    @Autowired
    IContainerActiveConfigService iContainerActiveConfigService;
    @Autowired
    IContainerConfigService iContainerConfigService;
    @Autowired
    IStageService iStageService;

    @Override
    public Map<String, List<TbNodeMetrics>> getMetrics(Long stageId, String serviceName, Date beginTime, Date endTime, int pageSize) {
        List<TbMetricsTask> lastMetricsTaskList = getLastMetricsTaskList(stageId, beginTime, endTime, pageSize);
        if (lastMetricsTaskList == null || lastMetricsTaskList.isEmpty()) {
            return Collections.emptyMap();
        }
        List<Long> lastMetricsTaskIdList = lastMetricsTaskList.stream().map(TbMetricsTask::getId).collect(Collectors.toList());
        List<TbNodeMetrics> lastNodeMetricsList = getLastNodeMetricsList(lastMetricsTaskIdList, serviceName);
        return lastNodeMetricsList.stream().collect(Collectors.groupingBy(TbNodeMetrics::getMetricsKey));
    }

    private List<TbNodeMetrics> getLastNodeMetricsList(List<Long> lastMetricsTaskidList, String serviceName) {
        TbNodeMetrics where = new TbNodeMetrics();
        where.setServiceName(serviceName);
        TbNodeMetricsCustomWhere customWhere = new TbNodeMetricsCustomWhere().setMetricsTaskIdList4In(lastMetricsTaskidList);
        return iNodeMetricsService.getList(where, customWhere, TbPageWhere.newPage4All());
    }

    private List<TbMetricsTask> getLastMetricsTaskList(Long stageId, Date beginTime, Date endTime, int pageSize) {
        TbMetricsTask where = new TbMetricsTask();
        where.setStageId(stageId);
        where.setCompleteState(TbPublicEnum.TASK_COMPLETE_STATE.COMPLETE.value);
        where.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        TbMetricsTaskCustomWhere customWhere = new TbMetricsTaskCustomWhere()
                .setExecuteTime4EndTime(MetricsUtil.dateToTaskExecuteTime(endTime))
                .setExecuteTime4BeginTime(MetricsUtil.dateToTaskExecuteTime(beginTime))
                .setOrderByType(TbPublicEnum.ORDER_BY_TYPE.ID_DESC.value);
        return iMetricsTaskService.getList(where, customWhere, TbPageWhere.newPage(0, pageSize));
    }


    @Override
    public List<String> getProxyNameList(Long stageId) {
        iStageService.getOrThrow(stageId);
        TbContainerActiveConfig tbContainerActiveConfig = iContainerActiveConfigService.selectLastOneByStageId(stageId);
        if (tbContainerActiveConfig == null) {
            return Collections.emptyList();
        }
        TbContainerConfig tbContainerConfig = iContainerConfigService.get(tbContainerActiveConfig.getOnlineConfigId());
        ContainerDataDTO containerDataDTO = FastJsonUtil.deserialize(tbContainerConfig.getConfigData(), ContainerDataDTO.class);
        List<String> serviceNameList = new ArrayList<>();
        containerDataDTO.getContainer().forEach(item -> serviceNameList.add(item.getProxyName()));
        return serviceNameList;
    }

}
